using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Resource.@string;
using MicroMsg.Scene;
using MicroMsg.Storage;
using MicroMsg.UI.UserContrl;
using MicroMsg.UI.Util;
using Microsoft.Phone.Shell;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace MicroMsg.UI.Page
{
	public class PeopleListPage : BasePage
	{
		private const string TAG = "PeopleListPage";
		private double oldTime;
		public string cuUser;
		private bool bIsNewMsgNotifyOn;
		private bool isInitPeopleList;
		private string mStrTopicOrigin = "";
		private string cuChatRoomNickName;
		private EventWatcher mChatRoomChangeWatcher;
		private ApplicationBar mPeopleBar;
		internal Grid Root;
		internal StackPanel LayoutRoot;
		internal StackPanel PeoplePanel;
		internal TextBlock PeopleNumTxt;
		internal PeopleList AddPeopleList;
		internal MMToggleSwitch toggleNewMsgNotify;
		private bool _contentLoaded;
		public PeopleListPage()
		{
			this.InitializeComponent();
			base.add_Loaded(new RoutedEventHandler(this.PeopleListPage_Loaded));
			base.add_Unloaded(new RoutedEventHandler(this.PeopleListPage_Unloaded));
		}
		private void LayoutRoot_Tap(object sender, GestureEventArgs e)
		{
			double nowMilliseconds = Util.getNowMilliseconds();
			if (Math.Abs(this.oldTime - nowMilliseconds) < 500.0)
			{
				return;
			}
			this.oldTime = nowMilliseconds;
			this.AddPeopleList.ResetDelState();
		}
		private void PeopleListPage_Unloaded(object sender, RoutedEventArgs e)
		{
			this.ChatRoomChangeUnit();
			if (ContactMgr.getUserType(this.cuUser) == ContactMgr.UserType.UserTypeChatRoom && this.bIsNewMsgNotifyOn != this.toggleNewMsgNotify.IsChecked)
			{
				this.bIsNewMsgNotifyOn = this.toggleNewMsgNotify.IsChecked;
				if (ChatRoomMgr.SetModChatRoomNotify(this.cuUser, this.bIsNewMsgNotifyOn))
				{
					ServiceCenter.sceneNewSync.doScene(4, syncScene.MM_NEWSYNC_SCENE_OTHER);
				}
			}
		}
		private void PeopleListPage_Loaded(object sender, RoutedEventArgs e)
		{
			this.ChatRoomChangeInit();
			PeopleList expr_0C = this.AddPeopleList;
			expr_0C.AddPepleClick = (RoutedEventHandler)Delegate.Combine(expr_0C.AddPepleClick, new RoutedEventHandler(this.AddPeopleBtn_Click));
			this.LayoutRoot.add_Tap(new EventHandler<GestureEventArgs>(this.LayoutRoot_Tap));
		}
		protected override void OnPageIn(object[] object_0)
		{
			if (object_0 != null && Enumerable.Count<object>(object_0) != 0)
			{
				this.cuUser = (object_0[0] as string);
			}
			base.set_ApplicationBar(this.GetPeopleApplicationBar());
			this.InitPeopleList();
			if (ContactMgr.getUserType(this.cuUser) == ContactMgr.UserType.UserTypeChatRoom)
			{
				this.InitToggle();
				return;
			}
			this.HideToggle();
		}
		protected override void OnPageOut()
		{
		}
		protected override void OnBackKeyPress(CancelEventArgs cancelEventArgs_0)
		{
			if (ChatPeopleButton.itemMode == ChatPeopleButton.PeopleButtonMode.delMode)
			{
				this.AddPeopleList.ResetDelState();
				cancelEventArgs_0.set_Cancel(true);
			}
			base.OnBackKeyPress(cancelEventArgs_0);
		}
		private void InitToggle()
		{
			this.bIsNewMsgNotifyOn = ChatRoomMgr.GetChatRoomNotify(this.cuUser);
			this.toggleNewMsgNotify.IsChecked = this.bIsNewMsgNotifyOn;
			this.toggleNewMsgNotify.Text = strings.Set_Common_ToggleNewMsgNotify;
			this.toggleNewMsgNotify.refreshShow();
		}
		private void HideToggle()
		{
			this.toggleNewMsgNotify.set_Visibility(1);
		}
		private void AddChatPeople()
		{
			List<string> list = new List<string>();
			if (ContactMgr.getUserType(this.cuUser) == ContactMgr.UserType.UserTypeChatRoom)
			{
				List<Contact> chatRoomMemberContactList = ChatRoomMgr.getChatRoomMemberContactList(this.cuUser);
				if (chatRoomMemberContactList != null && chatRoomMemberContactList.get_Count() >= 1)
				{
					using (List<Contact>.Enumerator enumerator = chatRoomMemberContactList.GetEnumerator())
					{
						while (enumerator.MoveNext())
						{
							Contact current = enumerator.get_Current();
							list.Add(current.strUsrName);
						}
						goto IL_8F;
					}
				}
				Log.smethod_1("PeopleListPage", "AddChatPeople,currentRoomInfo == null || currentRoomInfo.Count < 1");
				return;
			}
			list.Add(this.cuUser);
			list.Add(AccountMgr.getCurAccount().strUsrName);
			IL_8F:
			base.GoToPage("/Source/UI/Chat/AddPeoplePage.xaml", new object[]
			{
				AddPeoplePage.AddPeopleType.CreateFromChatPage,
				new AddPeopleComplete(this.AddPeopleComplete),
				list,
				this.cuUser
			}, false);
		}
		public void AddPeopleComplete(bool isSuccess)
		{
			if (!isSuccess)
			{
				return;
			}
			this.RefreshMember();
		}
		private void AddPeopleBtn_Click(object sender, RoutedEventArgs e)
		{
			this.AddChatPeople();
		}
		public void InitPeopleList()
		{
			if (this.isInitPeopleList)
			{
				return;
			}
			this.isInitPeopleList = true;
			this.PeopleNumTxt.set_Visibility(0);
			if (string.IsNullOrEmpty(this.cuUser))
			{
				Log.smethod_1("PeopleListPage", "InitPeopleList cuUser == null");
				return;
			}
			if (ContactMgr.getUserType(this.cuUser) == ContactMgr.UserType.UserTypeChatRoom)
			{
				Log.smethod_5("PeopleListPage", "It's a Chat room,getUserContactList start");
				List<Contact> chatRoomMemberContactList = ChatRoomMgr.getChatRoomMemberContactList(this.cuUser);
				Log.smethod_5("PeopleListPage", "getUserContactList end");
				if (chatRoomMemberContactList != null && chatRoomMemberContactList.get_Count() >= 1)
				{
					this.PeopleNumTxt.set_Text(string.Format(strings.PeopleNumText, chatRoomMemberContactList.get_Count()));
					using (List<Contact>.Enumerator enumerator = chatRoomMemberContactList.GetEnumerator())
					{
						while (enumerator.MoveNext())
						{
							Contact current = enumerator.get_Current();
							string nickName;
							if (current.strUsrName == AccountMgr.getCurAccount().strUsrName)
							{
								nickName = strings.ChatRoomMyself;
							}
							else
							{
								nickName = ContactHelper.GetContactDisplayName(current);
							}
							this.AddPeopleList.AddPeople(current.strUsrName, nickName);
						}
						goto IL_17A;
					}
				}
				Log.smethod_1("PeopleListPage", "InitPeopleList currentRoomInfo == null");
				return;
			}
			Log.smethod_5("PeopleListPage", "It's a single Chat");
			this.PeopleNumTxt.set_Text(string.Format(strings.PeopleNumText, 2));
			this.AddPeopleList.AddPeople(AccountMgr.getCurAccount().strUsrName, strings.ChatRoomMyself);
			this.AddPeopleList.AddPeople(this.cuUser, ContactHelper.getDisplayName(this.cuUser, null));
			IL_17A:
			Contact contact = StorageMgr.contact.method_0(this.cuUser);
			if (ContactMgr.IsFmessageUser(this.cuUser))
			{
				this.AddPeopleList.originMode = PeopleList.PeopleListMode.CommonModeMode;
				this.AddPeopleList.NowListMode = PeopleList.PeopleListMode.CommonModeMode;
				return;
			}
			if (contact.strChatRoomOwner == AccountMgr.curUserName)
			{
				this.AddPeopleList.originMode = PeopleList.PeopleListMode.EnableDelMode;
				this.AddPeopleList.NowListMode = PeopleList.PeopleListMode.EnableDelMode;
				return;
			}
			this.AddPeopleList.originMode = PeopleList.PeopleListMode.EnableAddMode;
			this.AddPeopleList.NowListMode = PeopleList.PeopleListMode.EnableAddMode;
		}
		private void SaveToContact_Click(object sender, EventArgs e)
		{
			if (ContactMgr.getUserType(this.cuUser) != ContactMgr.UserType.UserTypeChatRoom)
			{
				Log.smethod_5("PeopleListPage", "It's not a Chat room,SaveToContact_Click error");
				return;
			}
			Contact contact = StorageMgr.contact.method_0(this.cuUser);
			if (contact == null)
			{
				Log.smethod_1("PeopleListPage", "can't find contact for :" + this.cuUser);
				return;
			}
			if (ContactMgr.isContact(contact))
			{
				MessageBoxManager.ShowToast(strings.ChatContactExist, 2500, null, false);
				return;
			}
			if (!string.IsNullOrEmpty(contact.strNickName) && !(contact.strNickName == strings.ChatPageTitleDefaultText))
			{
				if (!ChatRoomMgr.addToContact(this.cuUser, contact.strNickName))
				{
					MessageBoxManager.ShowToast(strings.SaveChatRoomFail, 2500, null, false);
					return;
				}
				MessageBoxManager.ShowToast(strings.SaveChatRoomSuccess, 2500, null, false);
			}
			else
			{
				Dialog2BtnWithInputBox dialog2BtnWithInputBox = Dialog2BtnWithInputBox.Show(strings.SaveChatRoomDlgTitle, strings.SaveChatRoomDlgInfo, strings.COM_Confirm, strings.COM_Cancel, "", new Dialog2BtnWithInputBox.dismissWithClickBtn(this.AddToContact_Completed), null, null);
				if (contact.strNickName != strings.ChatPageTitleDefaultText)
				{
					dialog2BtnWithInputBox.txtIn.set_Text(contact.strNickName);
					return;
				}
			}
		}
		private void AddToContact_Completed(object sender, int nClickIndex)
		{
			if (nClickIndex == 0 && sender != null && sender is Dialog2BtnWithInputBox)
			{
				Dialog2BtnWithInputBox dialog2BtnWithInputBox = sender as Dialog2BtnWithInputBox;
				string text = dialog2BtnWithInputBox.txtIn.get_Text();
				if (string.IsNullOrEmpty(text))
				{
					MessageBoxManager.ShowToast(strings.RemarkChatRoomDlgTitle, 2500, null, false);
					return;
				}
				if (!ChatRoomMgr.addToContact(this.cuUser, text))
				{
					MessageBoxManager.ShowToast(strings.SaveChatRoomFail, 2500, null, false);
					return;
				}
				ChatRoomMgr.setChatRoomTopic(this.cuUser, text);
				MessageBoxManager.ShowToast(strings.SaveChatRoomSuccess, 2500, null, false);
			}
		}
		private void ModifyChatRoomTopic_Click(object sender, EventArgs e)
		{
			if (string.IsNullOrEmpty(this.cuUser))
			{
				return;
			}
			if (ContactMgr.getUserType(this.cuUser) != ContactMgr.UserType.UserTypeChatRoom)
			{
				Log.smethod_1("PeopleListPage", "invalid chatRoomName" + this.cuUser);
				return;
			}
			Contact contact = StorageMgr.contact.method_0(this.cuUser);
			if (contact == null)
			{
				Log.smethod_1("PeopleListPage", "can't find contact for :" + this.cuUser);
				return;
			}
			Dialog2BtnWithInputBox dialog2BtnWithInputBox = Dialog2BtnWithInputBox.Show(strings.ModifyChatTopicTitle, strings.ModifyChatTopicInfo, strings.COM_Confirm, strings.COM_Cancel, "", new Dialog2BtnWithInputBox.dismissWithClickBtn(this.ModifyChatRoomTopic_Completed), new TextChangedEventHandler(this.txtIn_TextChanged), null);
			string text = contact.strNickName;
			if ((string.IsNullOrEmpty(contact.strNickName) || contact.strNickName == strings.ChatDefaultTopic) && !string.IsNullOrEmpty(contact.strRemark))
			{
				text = contact.strRemark;
			}
			dialog2BtnWithInputBox.txtIn.set_Text(text);
			dialog2BtnWithInputBox.txtIn.set_SelectionStart(text.get_Length());
			this.cuChatRoomNickName = text;
		}
		private void txtIn_TextChanged(object sender, TextChangedEventArgs e)
		{
			Dialog2BtnWithInputBox dialog2BtnWithInputBox = sender as Dialog2BtnWithInputBox;
			if (dialog2BtnWithInputBox == null)
			{
				return;
			}
			if (dialog2BtnWithInputBox.txtIn == null)
			{
				return;
			}
			if (string.IsNullOrEmpty(dialog2BtnWithInputBox.txtIn.get_Text()))
			{
				dialog2BtnWithInputBox.btnLeft.set_IsEnabled(false);
			}
			else
			{
				if (string.IsNullOrEmpty(dialog2BtnWithInputBox.txtIn.get_Text().Trim()))
				{
					dialog2BtnWithInputBox.btnLeft.set_IsEnabled(false);
				}
				else
				{
					dialog2BtnWithInputBox.btnLeft.set_IsEnabled(true);
				}
			}
			if (ToolFunction.getStrDisLen(dialog2BtnWithInputBox.txtIn.get_Text()) <= 32)
			{
				dialog2BtnWithInputBox.btnLeft.set_IsEnabled(true);
				int arg_95_0 = dialog2BtnWithInputBox.txtContent.get_Text().get_Length();
				this.mStrTopicOrigin = dialog2BtnWithInputBox.txtIn.get_Text();
				return;
			}
			if (this.mStrTopicOrigin != null && this.mStrTopicOrigin.get_Length() < dialog2BtnWithInputBox.txtIn.get_Text().get_Length())
			{
				dialog2BtnWithInputBox.txtIn.set_Text(this.mStrTopicOrigin);
				dialog2BtnWithInputBox.txtIn.set_SelectionStart(dialog2BtnWithInputBox.txtIn.get_Text().get_Length());
			}
			this.mStrTopicOrigin = dialog2BtnWithInputBox.txtIn.get_Text();
			dialog2BtnWithInputBox.txtContent.set_Text(strings.ModifyChatTopicTooLong);
		}
		private void ModifyChatRoomTopic_Completed(object sender, int nClickIndex)
		{
			if (nClickIndex == 0 && sender != null && sender is Dialog2BtnWithInputBox)
			{
				Dialog2BtnWithInputBox dialog2BtnWithInputBox = sender as Dialog2BtnWithInputBox;
				string text = dialog2BtnWithInputBox.txtIn.get_Text();
				if (string.IsNullOrEmpty(text))
				{
					return;
				}
				if (text == this.cuChatRoomNickName)
				{
					return;
				}
				if (ChatRoomMgr.setChatRoomTopic(this.cuUser, text))
				{
					Contact contact = StorageMgr.contact.method_0(this.cuUser);
					if (contact != null && ContactMgr.isContact(contact))
					{
						ChatRoomMgr.addToContact(this.cuUser, text);
					}
					MessageBoxManager.ShowToast(strings.ModifyChatTopicSuccess, 2500, null, false);
					ServiceCenter.sceneNewSync.doScene(4, syncScene.MM_NEWSYNC_SCENE_OTHER);
					return;
				}
				MessageBoxManager.ShowToast(strings.ModifyChatTopicFail, 2500, null, false);
			}
		}
		private void ChatRoomChangeInit()
		{
			this.mChatRoomChangeWatcher = new EventWatcher(this, null, new EventHandlerDelegate(this.onChatRoomChange));
			EventCenter.registerEventWatcher(EventConst.ON_STORAGE_CHATROOM_ADD, this.mChatRoomChangeWatcher);
			EventCenter.registerEventWatcher(EventConst.ON_STORAGE_CHATROOM_DEL, this.mChatRoomChangeWatcher);
			EventCenter.registerEventWatcher(EventConst.ON_STORAGE_CHATROOM_MODIFY, this.mChatRoomChangeWatcher);
		}
		private void ChatRoomChangeUnit()
		{
			EventCenter.removeEventWatcher(EventConst.ON_STORAGE_CHATROOM_ADD, this.mChatRoomChangeWatcher);
			EventCenter.removeEventWatcher(EventConst.ON_STORAGE_CHATROOM_DEL, this.mChatRoomChangeWatcher);
			EventCenter.removeEventWatcher(EventConst.ON_STORAGE_CHATROOM_MODIFY, this.mChatRoomChangeWatcher);
		}
		private void onChatRoomChange(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			List<ChatRoom> listObject = evtArgs.getListObject<ChatRoom>();
			if (listObject == null)
			{
				return;
			}
			for (int i = 0; i < listObject.get_Count(); i++)
			{
				if (listObject.get_Item(i).strChatRoomName == this.cuUser)
				{
					this.RefreshMember();
					return;
				}
			}
		}
		private void RefreshMember()
		{
			Log.smethod_5("PeopleListPage", "RefreshMember");
			List<Contact> chatRoomMemberContactList = ChatRoomMgr.getChatRoomMemberContactList(this.cuUser);
			if (chatRoomMemberContactList != null && chatRoomMemberContactList.get_Count() >= 1)
			{
				this.PeopleNumTxt.set_Text(string.Format(strings.PeopleNumText, chatRoomMemberContactList.get_Count()));
				this.AddPeopleList.RefreshPeople(chatRoomMemberContactList);
				return;
			}
			this.AddPeopleList.RefreshPeople(null);
			Log.smethod_1("PeopleListPage", "RefreshForNewMember:currentRoomInfo == null");
		}
		public ApplicationBar GetPeopleApplicationBar()
		{
			if (this.mPeopleBar != null)
			{
				return this.mPeopleBar;
			}
			if (ContactMgr.IsFmessageUser(this.cuUser))
			{
				this.mPeopleBar = new ApplicationBar();
				this.mPeopleBar.set_Mode(1);
				this.mPeopleBar.set_IsMenuEnabled(true);
				this.mPeopleBar.set_IsVisible(true);
				this.mPeopleBar.set_Opacity(0.75);
				return this.mPeopleBar;
			}
			this.mPeopleBar = new ApplicationBar();
			this.mPeopleBar.set_Mode(1);
			this.mPeopleBar.set_IsMenuEnabled(true);
			this.mPeopleBar.set_IsVisible(true);
			this.mPeopleBar.set_Opacity(0.75);
			if (ContactMgr.getUserType(this.cuUser) == ContactMgr.UserType.UserTypeChatRoom)
			{
				ApplicationBarIconButton applicationBarIconButton = new ApplicationBarIconButton(new Uri("Resource/Img/Chat/modify_topic.png", 2));
				applicationBarIconButton.set_Text(strings.ModifyChatTopic);
				applicationBarIconButton.add_Click(new EventHandler(this.ModifyChatRoomTopic_Click));
				this.mPeopleBar.get_Buttons().Add(applicationBarIconButton);
				ApplicationBarIconButton applicationBarIconButton2 = new ApplicationBarIconButton(new Uri("Resource/Img/Chat/save.png", 2));
				applicationBarIconButton2.set_Text(strings.SaveToContact);
				applicationBarIconButton2.add_Click(new EventHandler(this.SaveToContact_Click));
				this.mPeopleBar.get_Buttons().Add(applicationBarIconButton2);
			}
			if (this.cuUser != null && this.cuUser.get_Length() > 0)
			{
				ApplicationBarMenuItem applicationBarMenuItem = new ApplicationBarMenuItem(strings.ChatBackground_Button);
				applicationBarMenuItem.add_Click(new EventHandler(this.changeBGButton_Click));
				this.mPeopleBar.get_MenuItems().Add(applicationBarMenuItem);
			}
			ApplicationBarMenuItem applicationBarMenuItem2 = new ApplicationBarMenuItem(strings.DeleteAndQuit);
			applicationBarMenuItem2.add_Click(new EventHandler(this.DeleteMenuItem_Click));
			this.mPeopleBar.get_MenuItems().Add(applicationBarMenuItem2);
			return this.mPeopleBar;
		}
		private void changeBGButton_Click(object sender, EventArgs e)
		{
			if (this.cuUser != null && this.cuUser.get_Length() > 0)
			{
				base.GoToPage("/Source/UI/Chat/background/SetBackgroundPage.xaml", new object[]
				{
					this.cuUser
				}, true);
			}
		}
		private void DeleteMenuItem_Click(object sender, EventArgs e)
		{
			if (string.IsNullOrEmpty(this.cuUser))
			{
				return;
			}
			Log.smethod_5("PeopleListPage", "DeleteMenuItem_Click");
			MMMessageBox mMMessageBox;
			if (ContactMgr.getUserType(this.cuUser) == ContactMgr.UserType.UserTypeChatRoom)
			{
				mMMessageBox = MMMessageBox.Show(strings.DeleteAndQuitRoomDlgTitle, strings.DeleteAndQuitRoomDlgInfo, true, strings.COM_Confirm, strings.COM_Cancel);
			}
			else
			{
				mMMessageBox = MMMessageBox.Show(strings.DeleteAndQuitDlgTitle, strings.DeleteAndQuitDlgInfo, true, strings.COM_Confirm, strings.COM_Cancel);
			}
			mMMessageBox.ReturnEvent = new DlgReturnEvent(this.OnDeleteAndQuitHandler);
		}
		private void OnDeleteAndQuitHandler(DlgReturnEventArg dlgReturnEventArg_0)
		{
			switch (MMMessageBox.GetUserClickResult(dlgReturnEventArg_0))
			{
			case 0:
				this.showProgressCover(strings.ChatQuiting);
				if (ContactMgr.getUserType(this.cuUser) == ContactMgr.UserType.UserTypeChatRoom)
				{
					ChatRoomMgr.quitChatRoom(this.cuUser);
					ServiceCenter.sceneNewSync.doScene(7, syncScene.MM_NEWSYNC_SCENE_OTHER);
				}
				else
				{
					ConversationMgr.delConversation(this.cuUser);
				}
				ChatContainerPage.CloseChatPage();
				if (base.get_NavigationService().get_CanGoBack())
				{
					base.get_NavigationService().GoBack();
				}
				break;
			case 1:
				break;
			default:
				return;
			}
		}
		public void showProgressCover(string strCoverTitle)
		{
			ProcessingCover.getProgressBar(this.Root, strCoverTitle, null, false, false, null);
		}
		public void hideProgressCover()
		{
			ProcessingCover.releaseProgressBar(this.Root);
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/UI/Chat/PeopleListPage.xaml", 2));
			this.Root = (Grid)base.FindName("Root");
			this.LayoutRoot = (StackPanel)base.FindName("LayoutRoot");
			this.PeoplePanel = (StackPanel)base.FindName("PeoplePanel");
			this.PeopleNumTxt = (TextBlock)base.FindName("PeopleNumTxt");
			this.AddPeopleList = (PeopleList)base.FindName("AddPeopleList");
			this.toggleNewMsgNotify = (MMToggleSwitch)base.FindName("toggleNewMsgNotify");
		}
	}
}
